﻿using System;
using System.Linq;
using System.Web;
using System.IO;
using EasyMvc.Utility;
using System.Web.Caching;

namespace EasyMvc.Config
{
    public class ConfigDataContext
    {
        public ConfigDataContext()
        {
    
        }

        public string GetConfigFile(string fileName)
        {
            string path = this.GetConfigFilePath(fileName);
            if (!File.Exists(path))
                return null;

            string content = string.Empty;
            if (Caching.Get(fileName) == null)
            {
                content = File.ReadAllText(path);
                Caching.Set(fileName, content, new CacheDependency(path));
            }
            else
            {
                content = Caching.Get(fileName) as string;
            }
            return content;
        }

        public void SaveConfigFile(string fileName, string content)
        {
            string path = this.GetConfigFilePath(fileName);
            File.WriteAllText(path, content);
        }

        public string GetConfigFilePath(string fileName)
        {
            return HttpContext.Current.Server.MapPath(string.Format("~/Config/{0}.Xml", fileName));
        }
    }
}
